#------------------------------------------------------------------------------- # Name: A453 Programming Project Sept 2014 # CA Material 2 - Task 3 # Purpose: Simple Maths quiz # # Author: Andrew Baker # # Created: 24/02/2015 # Copyright: (c) Andrew Baker 2015 # Licence: #------------------------------------------------------------------------------- #---------------------------------------- def readfile(classname): filename=classname+".txt" file = open(filename,"a+") file.seek(0) # to make sure the pointer is at the beginning results=file.readlines() file.close() return results #---------------------------------------- def list_names_in_file(): # Get names in class who have results for x in results: (name,score)=x.split(":") #takes every result and splits the name and score into the variables name and score res_dict[name]=score # name is the key in the dictionary res_dict[] # keys are unique. If it reads in a result for the same person, the score is overwritten # the net result is the keys provide a list of names, without duplication #---------------------------------------- def strip_old_results(): for i in res_dict.keys(): #go through all names count=0 for y in reversed(results): #work backwards through results file (name,score)=y.split(":") #takes every result and splits the name and score into the variables name and score if (i)==name: # compares the key name with the name read in from the results file count +=1 # keeps a count of how many results it has read in (working from the back of the file) for the key_name if count>3: # delete from results if the key_name has more than 3 results results.remove(y) #results now contains a maximum of 3 results for each pupil. They are their latest scores #---------------------------------------- def high_score_and_average(): for i in res_dict: #go through all key_names high_score=0 total=0 count=0 # for each key_name find their score in the results for x in results: # go through every result (name,score)=x.split(":") #takes every result and splits the name and score into the variables name and score if (i)==name: # compares the key name with the name read in from the results file count +=1 #keeps a count of the number of scores for that student total=total+int(score) #this is a running total of the scores that have been read in for that student if (int(score)>int(high_score)): # if the score read in is great than the highest score so far for the student, make this the new high score high_score=int(score) ave=total/count #calulate the average score for the student high_score_list.append((i,high_score)) #add this student's name and highest score to the GLOBAL high_score list ave_score_list.append((i,ave)) #add this student's name and average score to the GLOBAL ave_score list #---------------------------------------- def alpha_highest_score_high_to_low(): high_score_list.sort(key=lambda s: s[0]) #sorts the GLOBAL high score list using the student name as the key print() #prints a blank line print("Student's Highest Score") #prints a heading print("Name Score") #prints a heading #prints the list for name, score in (high_score_list): print("{} {}".format(name,score)) #---------------------------------------- def highest_score_high_to_low(): high_score_list.sort(key=lambda s: s[1]) #sorts the GLOBAL high score list using the highest score as the key print() #prints a blank line print("Highest Score") #prints a heading print("Score Name") #prints a heading #prints the list for name, score in reversed(high_score_list): print(" {} {}".format(score,name)) #---------------------------------------- def average_score_high_to_low(): ave_score_list.sort(key=lambda s: s[1]) #sorts the GLOBAL average score list using the average score as the key print() #prints a blank line print("Average Score") #prints a heading print("Ave Score Name") #prints a heading #prints the list for name, ave in reversed(ave_score_list): print(" {:.2f} {}".format(ave,name)) #---------------------------------------- classname="4TC" #input("Which class do you want to see?") results=readfile(classname) #Read in ALL the results for the class #Define Globally, Dictionary and Lists res_dict={} high_score_list=[] ave_score_list=[] #Analyse Data list_names_in_file() strip_old_results() high_score_and_average() #Outputs alpha_highest_score_high_to_low() highest_score_high_to_low() average_score_high_to_low()